package com.szyjjk.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.szyjjk.entity.SysImageCrawler;
import com.szyjjk.entity.SysImageCrawlerExample;
import com.szyjjk.mapper.SysImageCrawlerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @program: szyjjk
 * @description: 影像下载服务
 * @author: yangyue
 * @create: 2020-04-24 09:44
 */
@Service
@Transactional(readOnly = true)
public class SysImageCrawlerService {


    @Autowired
    private SysImageCrawlerMapper sysImageCrawlerMapper;


    /**
     * @Author yangyue  修改MOD标识
     * @Description //TODO
     * @Date 16:52 2019/6/1
     * @Param [id, today]
     * @return void
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public  boolean  todayMODTask(String id , int today){
        SysImageCrawler crawler=sysImageCrawlerMapper.selectByPrimaryKey(id);
        crawler.setModMarking(today);
        return editCrawler(crawler);
    }

    /**
     * @Author yangyue  修改MYD标识
     * @Description //TODO
     * @Date 16:52 2019/6/1
     * @Param [id, today]
     * @return void
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public  boolean  todayMYDTask(String id , int today){
        SysImageCrawler crawler=sysImageCrawlerMapper.selectByPrimaryKey(id);
        crawler.setMydMarking(today);
        return editCrawler(crawler);
    }

    /**
     * @Author yangyue
     * @Description //TODO 修改MOD 和MYD 的数据，如果原有的存在就的对应的添加和减
     * @Date 11:09 2019/7/23
     * @Param [crawlid, type, INCR]
     * @return boolean
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public  boolean  updateMasking(String crawlid , String fileName,int INCR){
        SysImageCrawler crawler=sysImageCrawlerMapper.selectByPrimaryKey(crawlid);
        if(fileName.indexOf("MOD")!=-1){
            int modnumber=0;
            if(crawler.getModMarking()!=null){
                modnumber=crawler.getModMarking();
            }
            modnumber=modnumber+INCR;
            //防止小于0
            if(modnumber<0){
                modnumber=0;
            }
            crawler.setModMarking(modnumber);
        }else if(fileName.indexOf("MYD")!=-1){
            int mydnumber=0;
            if(crawler.getMydMarking()!=null){
                mydnumber=crawler.getMydMarking();
            }
            //防止小于0
            mydnumber=mydnumber+INCR;
            if(mydnumber<0){
                mydnumber=0;
            }
            crawler.setMydMarking(mydnumber);
        }
        return editCrawler(crawler);
    }








    /**
     * @Author yangyue
     * @Description //TODO 爬虫任务分页查询
     * @Date 15:16 2019/5/22
     * @Param [pageNo, pageSize, begTime, endtime]
     * @return com.github.pagehelper.PageInfo<com.bkht.hyypt.hyybypt.entity.SysImageCrawler>
     **/
    public List<SysImageCrawler> findPageCrawler(Integer pageNo, Integer pageSize, String salit ) {
        SysImageCrawlerExample example=new SysImageCrawlerExample();
        example.setOrderByClause("download_state desc   limit " + pageNo + " , " + pageSize);
        return sysImageCrawlerMapper.selectByExample(example);
    }

    /**
     * @return java.util.List<com.bkht.hyypt.hyybypt.entity.SysImageCrawler>
     * @Author yangyue
     * @Description //TODO 查询所有爬虫任务
     * @Date 11:35 2019/6/1
     * @Param []
     **/
    public List<SysImageCrawler> findCrewlerAll() {
        return this.sysImageCrawlerMapper.selectByExample(null);
    }


    /**
     * @return com.bkht.hyypt.hyybypt.entity.SysImageCrawler
     * @Author yangyue
     * @Description //TODO 查询一个爬虫文件
     * @Date 17:33 2019/4/24
     * @Param [id]
     **/
    public SysImageCrawler findByIdCrawler(String id) {
        return sysImageCrawlerMapper.selectByPrimaryKey(id);
    }

    /**
     * @return java.lang.Boolean
     * @Author yangyue
     * @Description //TODO 添加爬虫数据
     * @Date 17:17 2019/4/24
     * @Param [sysProduct]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean saveCrawler(SysImageCrawler sysImageCrawler) {
        try {
            //todo 可填加后续逻辑
            sysImageCrawlerMapper.insertSelective(sysImageCrawler);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    /**
     * @return java.lang.Boolean
     * @Author yangyue
     * @Description //TODO 修改爬虫数据
     * @Date 17:27 2019/4/24
     * @Param [sysImageCrawler]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean editCrawler(SysImageCrawler sysImageCrawler) {
        try {
            //todo 可填加后续逻辑
            sysImageCrawlerMapper.updateByPrimaryKeySelective(sysImageCrawler);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @return java.lang.Boolean
     * @Author yangyue
     * @Description //TODO 删除爬虫数据
     * @Date 17:27 2019/4/24
     * @Param [id]
     **/
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public Boolean deleteCrawler(String id) {
        try {
            //todo 可填加后续逻辑
            sysImageCrawlerMapper.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * @Author yangyue
     * @Description //TODO 根据日期查询数量
     * @Date 15:01 2019/7/4
     * @Param [beginTime]
     * @return int
     **/
    public String findByDate(String beginTime) {
//		String tadayTask;
//		try {
//			tadayTask =sysImageCrawlerMapper.findByDate(beginTime);
//		} catch (Exception e) {
//			 e.printStackTrace();
//			 tadayTask="0";
//		}
        return null;
    }



    /**
     * @Author yangyue
     * @Description //TODO 根据类别(备注)查询爬虫
     * @Date 9:56 2019/6/27
     * @Param [type]
     * @return java.util.List<com.bkht.hyypt.hyybypt.entity.SysImageCrawler>
     **/
    public List<SysImageCrawler> getType(String type){
        SysImageCrawlerExample example=new SysImageCrawlerExample();
        example.createCriteria().andCrawlerRemarksLike("%"+type+"%");
        List<SysImageCrawler> ar=sysImageCrawlerMapper.selectByExample(example);
        return ar;
    }


    /**
     * @Author yangyue
     * @Description //TODO 根据卫星名字模糊查询 返回第一个
     * @Date 15:05 2019/7/4
     * @Param [satelliteVale]
     * @return com.bkht.hyypt.hyybypt.entity.SysImageCrawler
     **/
    public  SysImageCrawler getSatellite(String satelliteVale){
        SysImageCrawlerExample example=new SysImageCrawlerExample();
        example.createCriteria().andSatelliteLike("%"+satelliteVale+"%");
        try{
            SysImageCrawler resultCrawler=sysImageCrawlerMapper.selectByExample(example).get(0);
            if(resultCrawler==null){
                return null;
            }
        }catch (Exception e){
            return null;
        }
        return sysImageCrawlerMapper.selectByExample(example).get(0);
    }

    /**
     * @Author yangyue
     * @Description //TODO 查询卫星
     * @Date 14:40 2019/8/6
     * @Param [satelliteVale]
     * @return java.util.List<com.bkht.hyypt.hyybypt.entity.SysImageCrawler>
     **/
    public  List<SysImageCrawler> getallSatellite(String satelliteVale){
        SysImageCrawlerExample example=new SysImageCrawlerExample();
        example.createCriteria().andSatelliteLike("%"+satelliteVale+"%");
        return sysImageCrawlerMapper.selectByExample(example);
    }



    /**
     * @Author yangyue
     * @Description //TODO 根据ID 查询
     * @Date 17:49 2019/7/24
     * @Param [id]
     * @return com.bkht.hyypt.hyybypt.entity.SysImageCrawler
     **/
    public  SysImageCrawler findbyID(String id){
        SysImageCrawlerExample example=new SysImageCrawlerExample();
        example.createCriteria().andIdEqualTo(id);
        return sysImageCrawlerMapper.selectByExample(example).get(0);
    }



    
}